import java.awt.image.*;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;

	


public class GameEngine extends JPanel
{

    private int middle;
    
    private Image grassTile;
    private Image grassTile2;


    /* VARIABLES LOCALES*/

    private final int UPDATE_RATE = 10; 

    private Camera camera;
    private double startX = 0;
    private double startY = 0;

    private Point2D test;

    /* LIMITE POUR LE SCROLL */
    private int heightMargin = 15;
    private int widthMargin  = 15;
    
    private Point oldMousePos = null;
    /* vitesse de scroll */
    private double verticalScrollSpeed = 4;
    private double horizontalScrollSpeed = 4;
    

    private Image bimage;
    private Graphics2D offg;


    public GameEngine(int width, int height)
    {
	super();
	
	this.setPreferredSize(new Dimension(width,height));
	//this.addMouseMotionListener(new ScrollListener()); //plus besoin pour l'instant, mais faut garder pour utiliser le code pour un autre type de listener

	camera = new Camera(new Point2D(startX,startY),new Dimension(width,height));

	test = new Point2D(width/2, height/2);
	
	middle = width / 2;
	

	initImages();

    }

    ///////////////////////////////////////////////////:
    

    private void initImages() //charge les images .png
    {
	while (grassTile == null || grassTile2 == null ){
	    grassTile  = this.getToolkit().getImage("grassTile_1.png");
	    grassTile2 = this.getToolkit().getImage("grassTile_2.png");
	    
	}

    }

    ///////////////////////////////////////////////////:
    private void updateCameraPosition()
    {
	Point mousePosTmp = (this.getMousePosition() != null) ? this.getMousePosition() : oldMousePos;
	if (mousePosTmp != null){
	    
	    
	    double x = mousePosTmp.getX();
	    double y = mousePosTmp.getY();
	    
	    double scrollx = 0;
	    double scrolly = 0;
		
	    if ( (this.getWidth() - x) < widthMargin)
		scrollx = -horizontalScrollSpeed;
	    else
		if ( x < widthMargin)
		    scrollx = horizontalScrollSpeed;
	    
	    if ( (this.getHeight() - y) < heightMargin)
		scrolly = -verticalScrollSpeed;
	    else
		if ( y < heightMargin)
		    scrolly = verticalScrollSpeed;
	    
	    
	    camera.move(scrollx,scrolly);
	    oldMousePos = new Point((int)x,(int)y);
	}
    }

    public void updateCycle()
    {
	/////////////////////////////
	//////BOUCLE D'UPDATE////////
	/////////////////////////////

	
	while(true){
	    /* ICI FAIRE TOUS LES APPELS AUX FONCTIONS D'UPDATE */
	    
	    updateCameraPosition();
		
	    repaint();
	    
	    try{
		Thread.sleep(UPDATE_RATE);
	    }catch(Exception e){System.out.println("pb");}
	}
    }
    
    public void update(Graphics g)
    {
	
	paint(g);
	
    }

    public void paint(Graphics g)
    {
	if (offg == null){
	    
	    bimage = createImage(getWidth(),getHeight());
	    offg   = (Graphics2D)bimage.getGraphics();
	    
	    /** Désactivation de l'anti-aliasing */
	    offg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
	    offg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
	    /** Demande de rendu rapide */
	    offg.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
	    offg.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
	    offg.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
	    offg.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
	    
	    
	}

	
	offg.clearRect(0,0,getWidth(),getHeight());  //vide l'image précédente
	
	for (int i = 0; i < 400; i+=40){
	    for (int j = 0; j < 400; j+=40)
		{
		    
		    
		    offg.drawImage(grassTile,middle + i-j + (int)camera.getX(),100+ (i+j + (int)camera.getY())/2 ,null);
		   
		}



	}
	
	g.drawImage(bimage,0,0,this);
    }
}